<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Order;
use App\Models\Box;
use App\Models\BoxCare;
use App\Models\ActivityJoin;
use App\Models\EnergyCome;
use App\Models\BoxRetreat;
use App\Models\UsersWechat;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WelcomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }

    public function getIndex(Request $request)
    {
        $wechatCount = UsersWechat::count();
        $vipCount = UsersWechat::where('type','>',0)->count();
        $OrderCount = Order::where('pay_status',Order::STATUS_SUCCESS)->sum('total_amount') /100;

        $box0 = Box::count();
        $box1 = Box::where('status',2)->count();
        $box2 = Box::where('status',4)->count();
        $box3 = Box::where('status',1)->whereDate('delivery_at','<',date('Ymd', strtotime("+15 day")))->count('id');//date('Ymd', strtotime("+15 day"))
        $box4 = BoxCare::count('id');
        $box5 = Box::where('to_wechat_id','>',0)->count('id');
        $joinCount = ActivityJoin::count('id');
        $boxCount = [
            'box0'=>$box0,
            'box1'=>$box1,
            'box2'=>$box2,
            'box3'=>$box3,
            'box4'=>$box4,
            'box5'=>$box5,
        ];
        $weeks = getWeeks();

        $EnergyCount = EnergyCome::where('status',2)->count('id');

        // 换货申请
        $BoxRetreatCount = BoxRetreat::where('status',1)->orWhere('status',2)->count('id');

        //  七日用户数
        $count = DB::select("select DATE_FORMAT(created_at, '%Y-%m-%d') as created_at,COUNT(*) as count FROM `users_wechat` where created_at >= ".$weeks[1]." group by DATE_FORMAT(created_at, '%Y-%m-%d')");
        $Arr = [];
        $ArrUser = [];
        foreach ($count as $key => $value) {
            $Arr[$value->created_at] = $value->count;
        }
        foreach ($weeks as $key => $value) {
            if (array_key_exists($value,$Arr)) {
                $ArrUser[$value]=$Arr[$value];
            }else{
                $ArrUser[$value]=0;
            }
        }

        //  七日订单数
        $countOrder = DB::select("select DATE_FORMAT(created_at, '%Y-%m-%d') as created_at,COUNT(*) as count FROM `order` where pay_status=".Order::STATUS_SUCCESS." and created_at >= ".$weeks[1]." group by DATE_FORMAT(created_at, '%Y-%m-%d')");
        $Arr2 = [];
        $ArrOrder = [];
        foreach ($countOrder as $key => $value) {
            $Arr2[$value->created_at] = $value->count;
        }
        foreach ($weeks as $key => $value) {
            if (array_key_exists($value,$Arr2)) {
                $ArrOrder[$value]=$Arr2[$value];
            }else{
                $ArrOrder[$value]=0;
            }
        }
        $data = compact('wechatCount','vipCount','OrderCount','boxCount','ArrUser','ArrOrder','joinCount','EnergyCount','BoxRetreatCount');
        return view('admin/welcome', $data);
    }

}
